﻿using StudentManagerModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentManagerDAL.Helper;
using System.Data;
using System.Data.SqlClient;

namespace StudentManagerDAL
{
    public class AdminService
    {
        public Admin AdminLogin(Admin admin)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append("select LoginId,LoginPwd,AdminName from Admins where LoginId='{0}' and LoginPwd='{1}'");

            //设置参数
            string sql = string.Format(sqlBuilder.ToString(), admin.LoginId, admin.LoginPwd);
            SqlDataReader sqlDataReader = SQLHelper.GetReader(sql);

            if (sqlDataReader.Read())
            {
                admin.LoginId = (int)sqlDataReader["LoginId"];
                admin.LoginPwd = sqlDataReader["LoginPwd"].ToString();
                admin.AdminName = sqlDataReader["AdminName"].ToString();
            }
            else
            {
                admin = null;
            }
            return admin;
        }

        // 修改管理员密码
        public int ModifyPwd(Admin objAdmin)
        {
            string sql = "update Admins set LoginPwd='{0}' where LoginId={1}";
            sql = string.Format(sql, objAdmin.LoginPwd, objAdmin.LoginId);
            try
            {
                return SQLHelper.Update(sql);
            }
            catch (SqlException)
            {
                throw new Exception("应用程序和数据库连接出现问题！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
